//
//  BossHomePullView.m
//  DingDongZP
//
//  Created by 杨政涛 on 2016/12/29.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "BossHomePullView.h"


#define kRowHeight  37

@interface BossHomePullView ()<UITableViewDataSource, UITableViewDelegate,UIGestureRecognizerDelegate>

@property (strong, nonatomic) UITableView   *pullTableView;
@property (strong, nonatomic) NSString      *currentTitleSring;
@property (assign, nonatomic) BOOL          isShow;
@property (strong, nonatomic) UIView        *pullView;
@property (assign, nonatomic) NSInteger     recordNumber;  //用于记录点击了哪个


@end

@implementation BossHomePullView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.currentTitleSring = @"";
        
        self.pullView = [[UIView alloc] init];
        self.pullView.top = 0;
        self.pullView.centerX = (kScreenWidth-199)/2;
        self.pullView.width  = 199.f;
        self.pullView.height = 0.f;
        [[self.pullView layer] setShadowOffset:CGSizeMake(1, 1)];
        [[self.pullView layer] setShadowRadius:5];
        [[self.pullView layer] setShadowOpacity:1];
        [[self.pullView layer] setShadowColor:[UIColor grayColor].CGColor];
        [self addSubview:_pullView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        [tap addTarget:self action:@selector(tapView)];
        
        self.pullTableView = [[UITableView alloc] init];
        
        if ([self.pullTableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.pullTableView setSeparatorInset:UIEdgeInsetsZero];
        }
        if ([self.pullTableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [self.pullTableView setLayoutMargins:UIEdgeInsetsZero];
        }
        
        [self.pullTableView setDelegate:self];
        [self.pullTableView setDataSource:self];
        
        self.pullTableView.rowHeight = kRowHeight;
        self.pullTableView.scrollEnabled = true;
        self.pullTableView.frame = CGRectMake(0, 0, 199.f, 0);
        [self.pullView addSubview:_pullTableView];
        
        self.isShow = NO;
    }
    return self;
}

#pragma mark --- Public

- (BOOL)isShowPullView
{
    return self.isShow;
}

- (void)showPullView {
    
    self.isShow = YES;
    self.hidden = NO;
    [UIView animateWithDuration:.5f animations:^{
        
        if ([self.titles count] < 5) {
            self.pullView.height = kRowHeight * [_titles count];
            self.pullTableView.height = kRowHeight * [_titles count];
        } else {
            self.pullTableView.height = kRowHeight * 4;
            self.pullView.height = kRowHeight * 4;
        }
        
    } completion:^(BOOL finished) {
    }];
    
    
    [self.pullTableView reloadData];
}

- (void)setTitles:(NSArray *)titles
{
    if (_titles != titles) {
        _titles = titles;
        if ([_titles count] < 5) {
            self.pullTableView.height = kRowHeight * [_titles count];
        } else {
            self.pullTableView.height = kRowHeight * 4;
        }
        
        [self.pullTableView reloadData];
    }
}

- (void)dismissPullView
{
    self.isShow = NO;
    WEAKSELF
    [UIView animateWithDuration:.2f animations:^{
        weakSelf.pullView.height = 0.f;
        weakSelf.pullTableView.height = 0.f;
        weakSelf.layer.shadowPath = nil;
    } completion:^(BOOL finished) {
        if (finished) {
            self.hidden = YES;
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titles.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    if ([self.titles count] > indexPath.row) {
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:13.f];
        cell.textLabel.text = self.titles[indexPath.row];
        cell.textLabel.textColor = COLOR_FONT_LIGHT_GRAY;
        
        if ([self.currentTitleSring length] == 0) {
            self.currentTitleSring = self.titles[0];
        }
        
        if ([self.titles[indexPath.row] isEqualToString:self.currentTitleSring]) {

            cell.textLabel.attributedText = [DDUtils image:@"homeGou" ContentText:[NSString stringWithFormat:@"%@",self.titles[indexPath.row]]];
            cell.textLabel.textColor = COLOR_FONT_LIGHT_GREEN;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.currentTitleSring = self.titles[indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = COLOR_FONT_LIGHT_GRAY;
    NSInteger row = [self.titles indexOfObject:_currentTitleSring];
    UITableViewCell *beforeCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
    beforeCell.textLabel.textColor = COLOR_FONT_MAIN_GREEN;
    
    if (self.seletedTitleBlock) {
        [self dismissPullView];
        self.seletedTitleBlock(indexPath.row);
        _recordNumber = indexPath.row;
    }
}

- (void)tapView
{
    if (self.seletedTitleBlock) {
        [self dismissPullView];
        self.seletedTitleBlock(_recordNumber);
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([[touch view] isKindOfClass:[BossHomePullView class]]) {
        return YES;
    } else {
        return NO;
    }
}


@end
